//
//  AppDelegate.swift
//  XtWeiBo
//
//  Created by 徐腾 on 2017/7/18.
//  Copyright © 2017年 徐腾. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = WBMainViewController()
        
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }

}


// MARK: - 从服务器加载应用信息
extension AppDelegate {
    
    fileprivate func loadAppInfo() {
        
        // 模拟异步加载
        DispatchQueue.global().async {
            
            // 1> 获取信息的 url
            let path = Bundle.main.url(forResource: "main.json", withExtension: nil)
            
            // 2> 转 data
            let data = NSData(contentsOf: path!)
            
            // 3> 写入磁盘
            let docDior = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let filePath = (docDior as NSString).appendingPathComponent("main.json")
            
            // 直接保存到沙盒，等待下一次程序启动时使用
            data?.write(toFile: filePath, atomically: true)
            
            print("应用程序加载完成 \(filePath)")
            
        }
        
    }
}



